package edu.sysu.demo.petstore.dog.key;

import com.google.common.io.BaseEncoding;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;

abstract class KeyConverter {

  private final BaseEncoding baseEncoding;

  private KeyFactory keyFactory;

  KeyConverter(BaseEncoding baseEncoding, Provider provider)
      throws NoSuchAlgorithmException {
    this.baseEncoding = baseEncoding;

    this.keyFactory = KeyFactory.getInstance("RSA", provider);
  }

  <T extends java.security.Key> T convert(String base64Encoded) throws GeneralSecurityException {
    byte[] bytes = baseEncoding.decode(base64Encoded);
    return convert(bytes, keyFactory);
  }

  protected abstract <T extends java.security.Key> T convert(byte[] bytes, KeyFactory keyFactory)
      throws GeneralSecurityException;
}
